
window.onload=function () {

    let isNewPic=false;
    let isScale=false;
    let initPic=document.getElementById("upload").src;
    let http=new XMLHttpRequest();
    let input1=document.getElementById("compyname");
    let input2=document.getElementById("name");
    let input3=document.getElementById("phone");


    document.getElementById("send").addEventListener("click",()=>{


        console.log("OOOOK");
        let input1=document.getElementById("compyname");
        let input2=document.getElementById("name");
        let input3=document.getElementById("phone");
        let imgurl=document.getElementById("flieSend").files[0];
        let form=new FormData();
        form.append("compyname",input1.value);
        form.append("name",input2.value);
        form.append("phone",input3.value);
        form.enctype="multipart/form-data";

        if(imgurl){

            form.append("img",imgurl);
        }

        if(input1.value.length>=2&&input2.value.length>=2&&input3.value.length>=6) {

            var xhr = null;
            if (XMLHttpRequest) {
                xhr = new XMLHttpRequest();
            }

            xhr.open("POST", "/uploadimg", true);//设置提交方式，url，异步提交
            xhr.onload = function () {
                var data = xhr.responseText;

                let url = "/toSendInfo?company=" + input1.value + "&peoplename=" + input2.value + "&phone=" + input3.value + "&imageurl=" + JSON.parse(data).uploadStatus;
                xhr.open("POST", url, true);
                xhr.send();
                xhr.onload = function () {
                    var tgd = xhr.responseText;
                    if (JSON.parse(tgd).status == "OK") {
                        alert("录入成功!");
                        input1.value="";
                        input2.value="";
                        input3.value="";

                    }
                }

            }
            xhr.send(form);
        }
    });




    document.getElementById("upload").addEventListener("click",()=>{

        document.getElementById("flieSend").click();

    });



    document.getElementById("flieSend").addEventListener("change",()=>{


        var reader = new FileReader();
        let file=document.getElementById("flieSend").files[0];
        reader.readAsDataURL(file);
        reader.onload = function(e) {
            var newUrl = this.result;
            console.log(newUrl);
            document.getElementById("upload").src=newUrl;
            isNewPic=true;
        };

    });


    document.getElementById("toclose").addEventListener("click",()=>{

        if(isNewPic) {
            document.getElementById("upload").src=initPic;
        }

    });

    document.getElementById("toscale").addEventListener("click",()=>{

        if(isNewPic){

            //放大

            let target=document.createElement("div");
            target.className="back";
            let img=document.createElement("img");
            img.src=document.getElementById("upload").src;
            img.className="show";

            target.appendChild(img);
            document.getElementById("temp").appendChild(target);
            let imgheight=img.offsetHeight;
            img.style.marginTop=-imgheight/2+"px";
            console.log(imgheight+"px");

            target.addEventListener("click",()=>{

                target.parentNode.removeChild(target);


            },false);
        }


    });

}

